<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="examination.AccountBean"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>考试信息表</title>
	<link rel="stylesheet" href="layui/css/layui.css" media="all"/>
	<link rel="stylesheet" href="css/soulTable.css" media="all"/>
	<script src="layui/layui.js"></script>
	
</head>
<body>
	<h2 align=center>考试信息表</h2>
	<table id="myTable" lay-filter="exam_info"></table>
	<script type="text/html" id="toolbar">
		<div><button class="layui-btn layui-btn-sm" lay-event="clearFilter">清除所有筛选条件</button></div>
	</script>
	<!-- 操作条模板 -->
	<%
	 /*判断用户类型*/
	 AccountBean account = (AccountBean)session.getAttribute("account");
	String staffNo="";
	if(account!=null){
		staffNo = account.getStaffNo();
	}else{
		%>
		<script>
		layui.use('layer', function(){
			  var layer = layui.layer;
			  layer.alert('请先登录', function(index){
					window.location.href="login.jsp";
				}); 
			});  
		</script>
		<%	
		return;//不继续向下执行
	}
	 
	 
	
	%>
	<script type="text/html" id="bar">
		<%
		if(account.getType()==1)
		{
		%>
			<a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
			<a class="layui-btn layui-btn-xs" lay-event="arrange">安排监考</a>
		<%
		}
		else{
		%>
			<a class="layui-btn layui-btn-xs" lay-event="apply">申请</a>
		<%
		}
		%>
	</script>
	<script type="text/javascript" src="layui/layui.js"></script>
	<script>
		layui.config({
		  base: 'ext/',   // 模块目录
		}).extend({             // 模块别名
		  soulTable: 'soulTable'
		});
		
		layui.use(['form', 'table','soulTable'], function () {
	   	var table = layui.table,
	  	soulTable = layui.soulTable;
	   	
	   	
	   	// 后台分页
	   	table.render({
			elem: '#myTable'
		    ,id: 'myTable'
		    //,url: 'ExamQueryServlet?method=examNo&examNo=10002'
		    ,url: 'ExamQueryServlet'
		    ,height: 500
		    ,toolbar: '#toolbar'
		    ,page: true
		    ,cols: [[
			  {type: 'checkbox', fixed: 'left'},
			  {field: 'examNo', title: '考试编号', width: 150 , filter: true},
		      {field: 'status', title: '考试状态', width: 110, sort: true, filter: true},
		      {field: 'term', title: '学期', width: 150, sort: true, filter: true},
		      {field: 'collegeName', title: '学院', width: 100 , filter: true},
		      {field: 'courseName', title: '课程名', width: 120,  filter: true},
		      {field: 'classNo', title: '考试班级', width: 200, filter: true},
		      {field: 'date', title: '考试日期', width: 110,  filter: true},
		      {field: 'time', title: '考试时间', width: 100, filter: false},
		      {field: 'location', title: '考试地点', width: 100, filter: true},
		      {field: 'invigilatorNames', title: '监考老师', width: 200, filter: true},
		      {field: 'workload', title: '监考工分', width: 100, filter: true},
		      {field: 'need', title: '监考所需人数', width: 100, filter: true},
		      {field: 'applyDate', title: '申请日期', width: 200, filter: true},
		      {title: '操作', width: 150, fixed: 'right', templet: '#bar'}
		    ]]
		    ,done: function () {
		      soulTable.render(this)
		    }
	   		,parseData: function(res){ //res 即为原始返回的数据
	   			var exams = res.data;
	   			
	   			for(var examIndex in exams){
	   				var invigilatorNames=[];
	   				invigilators = exams[examIndex].invigilator
	   				
	   				for(var index in invigilators){
	   					console.log(invigilators[index]);
	   					if(invigilators[index]!==null){
	   						invigilatorNames.push(invigilators[index].name);
	   					}
		   				
		   			}
	   				console.log(invigilatorNames);
	   				exams[examIndex].invigilatorNames = invigilatorNames
	   				
	   			}
				
	   			
		   	    var newData={
		   	      "code": res.code, //解析接口状态
		   	      "msg": res.msg, //解析提示文本
		   	      "count": res.count, //解析数据长度
		   	      "data": res.data //解析数据列表
		   	    };
	   		return newData;
	   	  }
		  });
		  
		  table.on('toolbar(myTable)', function(obj){
		    if (obj.event === 'clearFilter') {
		      // 清除所有筛选条件并重载表格
		      // 参数: tableId
		      soulTable.clearFilter('myTable')
		    }
		  });
		  
		  //监听行工具事件   
          table.on('tool(exam_info)', function(obj){
              var data = obj.data;
              var httpRequest = new XMLHttpRequest();
              if(obj.event === 'apply'){
                  layer.confirm('是否申请成为监考', function(index){
                      
                  });
                  layer.confirm('请选择申请主监考还是副监考', {
                	  btn: ['主监考', '副监考', '取消'] //可以无限个按钮
                	}, function(index, layero){
                		//主监考
                		var para="staffNo=" + "<%=staffNo %>" + "&examNo=" + data.examNo + "&isMain=true";
                        window.location.href="SetInvigilatorServlet?"+para;//跳转
                        
                	}, function(index){
                	  	//副监考
                		var para="staffNo=" + "<%=staffNo %>" + "&examNo=" + data.examNo + "&isMain=false";
                        window.location.href="SetInvigilatorServlet?"+para;//跳转
                        
                	}, function(index){
                	  	//取消

                	});
              } 
              else if(obj.event === 'arrange'){
             	   var para="examNo=";
                   para+=data.examNo;
                   window.location.href="invigilator_arrange.jsp?"+para;//跳转
              }
              else if(obj.event === 'edit'){
	              //httpRequest.open('POST', 'exam_update_form.jsp', true);
	              //httpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	              //httpRequest.send('examNo='+data.examNo);
	              var para="examNo=";
                  para+=data.examNo;
                  window.location.href="exam_update_form.jsp?"+para;//跳转 
              } 
          });
		})
	</script>
	
</body>
</html>